/****************************************************
 * Description: DAOimpl for 角色权限
 * Copyright:   Copyright (c) 2013
 * Company:     beiwaionline
 * @author      bfsu
 * @version     1.0
 * @see
	HISTORY
    *  2013-08-08 bfsu Create File
**************************************************/

package com.bfsuolframework.security.dao.hibernate;

import java.util.List;

import com.bfsuolframework.security.dao.RolePrivilegeDAO;
import com.bfsuolframework.security.entity.RolePrivilege;
import org.springframework.stereotype.Repository;

import com.bfsuolframework.core.dao.HibernateDAOSupport;
import com.bfsuolframework.core.dao.support.HqlParameter;

@Repository
public class RolePrivilegeDAOImpl extends HibernateDAOSupport<RolePrivilege> implements RolePrivilegeDAO {

    //通过 所属角色主键 查询 角色权限 集合
    private final String FIND_ROLEPRIVILEGE_BYROLEID = "FROM RolePrivilege rolePrivilege WHERE rolePrivilege.role.id = :roleId";
    
    // 通过角色id和权限code获得角色权限
    private final String FIND_BYROLEPRI = "from RolePrivilege rp where rp.role.id=:rid and rp.privilegeCode=:pcode";
    
    // 删除角色相关的所有角色权限
    private final String DEL_BY_ROLE = "delete RolePrivilege rp where rp.role.id=:rid";
	
    /* (non-Javadoc)
     * @see RolePrivilegeDAO#findRolePrivilegesByRoleId(java.lang.Long)
     */
    public List<RolePrivilege> findRolePrivilegesByRoleId(Long roleId){
        return findAll(FIND_ROLEPRIVILEGE_BYROLEID, HqlParameter.newInstance("roleId", roleId));
    }
	
    /* (non-Javadoc)
     * @see RolePrivilegeDAO#findByRolePri(java.lang.Long, java.lang.String)
     */
    public RolePrivilege findByRolePri(Long roleid, String pcode){
    	List<RolePrivilege> list = findAll(FIND_BYROLEPRI, HqlParameter.newInstance("rid",roleid).addParameter("pcode", pcode));
    	return list.size() == 1 ? list.get(0) : null;
    }
    
    /* (non-Javadoc)
     * @see RolePrivilegeDAO#delByRole(java.lang.Long)
     */
    public boolean delByRole(Long roleid){
    	try {
    		executeHql(DEL_BY_ROLE, HqlParameter.newInstance("rid",roleid));
    		return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
    }

}